package atul.r.raut;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListItemAdapter extends ArrayAdapter<ListItem> 
{
	 	Context context; 
	    int layoutResourceId;    
	    List<ListItem> items = null;
	    
	    public ListItemAdapter(Context context, int layoutResourceId, List<ListItem> items)
	    {
	        super(context, layoutResourceId, items);
	        this.layoutResourceId = layoutResourceId;
	        this.context = context;
	        this.items = items;
	    }

	    @Override
	    public View getView(int position, View convertView, ViewGroup parent) 
	    {
	        View row = convertView;
	        ListItemHolder holder = null;
	        
	        if(row == null)
	        {
	            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
	            row = inflater.inflate(layoutResourceId, parent, false);
	            
	            holder = new ListItemHolder();
	            holder.leftImage = (ImageView)row.findViewById(R.id.leftImage);
	            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
	            holder.rightImage = (ImageView)row.findViewById(R.id.rightImage);
	            
	            row.setTag(holder);
	        }
	        else
	        {
	            holder = (ListItemHolder)row.getTag();
	        }
	        
	        ListItem listitem = (ListItem)items.get(position);
	        holder.leftImage.setImageResource(listitem.leftImage);
	        holder.txtTitle.setText(listitem.title);
	        holder.rightImage.setImageResource(listitem.rightImage);
	        
	        return row;
	    }
	    
	    static class ListItemHolder
	    {
	        ImageView leftImage;
	        TextView txtTitle;
	        ImageView rightImage;
	    }
}
